/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.HashMap;

import com.elf.gwt.common.client.model.AccountModel;
import com.elf.gwt.common.client.model.CategoryModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;


/**
 * 
 */

public class BreadcrumbComponent extends StoreSimpleComponent
{
	protected String token;
	protected HashMap<String, String> hash;
	protected String delimiter;

	protected Grid grid;
	protected HorizontalPanel titlePanel;
	protected HorizontalPanel welcomePanel;
	protected Anchor homeAnchor;
	/**
	 * Constructor
	 */

	public BreadcrumbComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);
		hash = new HashMap<String, String>();

		grid = new Grid(1,2);
		grid.setStyleName(getHandle() + "DockPanel");
		titlePanel = new HorizontalPanel();
		titlePanel.addStyleName(getHandle() + "TitlePanel");
		welcomePanel = new HorizontalPanel();
		welcomePanel.addStyleName(getHandle() + "WelcomePanel");

		homeAnchor = new Anchor(controller.getStoreConstants().BreadcrumbComponentHome(), false, controller.getHomeUrl());
		homeAnchor.addStyleName(getHandle() + "Link");

		delimiter = controller.getStoreConstants().BreadcrumbComponentDelimiter();

		grid.setWidget(0, 0, titlePanel);
		grid.setWidget(0, 1, welcomePanel);
		grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);

		panel.setWidget(0, 0, grid);
	}


	public void build()
	{
		hash.clear();
		titlePanel.clear();

		//Breadcrumbs
		String handle = "";
		String[] split = null;
		if (token != null && token.length() > 0)
		{
			split = token.split("--");
			handle = split[0];
		}

		if (handle.length() > 0)
		{
			if (handle.equals("OrderComponent"))
			{
				addBreadcrumb(handle, controller.getComponent(handle).getTitle(), true);
				addDelimiter();
				addBreadcrumb("OrdersSummaryComponent", controller.getComponent("OrdersSummaryComponent").getTitle(), false);
				addDelimiter();
				addBreadcrumb("AccountComponent--ViewAccount", storeController.getStoreConstants().AccountComponentAccountTitle(), false);
			}
			else if (handle.equals("OrdersSummaryComponent"))
			{
				addBreadcrumb(handle, controller.getComponent(handle).getTitle(), true);
				addDelimiter();
				addBreadcrumb("AccountComponent--ViewAccount", storeController.getStoreConstants().AccountComponentAccountTitle(), false);
			}
			else if (handle.equals("AccountComponent"))
			{
				addBreadcrumb(handle, controller.getComponent(handle).getTitle(), true);
				if (StoreComponentController.accountComponent.getAction().equals(AccountComponent.AddressBook) ||
						StoreComponentController.accountComponent.getAction().equals(AccountComponent.ChangePassword) ||
						StoreComponentController.accountComponent.getAction().equals(AccountComponent.EditAccount))
				{
					addDelimiter();
					addBreadcrumb("AccountComponent--ViewAccount", storeController.getStoreConstants().AccountComponentAccountTitle(), false);
				}
			}
			else if (!(handle.equals("ManufacturersComponent") || 
					handle.equals("CategoriesSummaryComponent") ||
					handle.equals("ProductComponent") ||
					handle.equals("CurrenciesComponent")
			))
			{
				addBreadcrumb(handle, controller.getComponent(handle).getTitle(), true);
			}

			if (!(
					handle.equals("OrderComponent") ||
					handle.equals("OrdersSummaryComponent") ||
					handle.equals("AccountComponent") ||
					handle.equals("CheckoutComponent") ||
					handle.equals("CartComponent")
			))
			{
				if (StoreComponentController.productComponent != null && StoreComponentController.productComponent.getProductModel().getId() > 0)
				{
					if (titlePanel.getWidgetCount() > 0) addDelimiter();
					addBreadcrumb("ProductComponent--" + StoreComponentController.productComponent.getProductModel().getId() +
							"--" + StoreComponentController.productComponent.getProductModel().getCategoryId() + 
							"--" + StoreComponentController.manufacturersComponent.getManufacturerModel().getId(),
							StoreComponentController.productComponent.getProductModel().getName(), titlePanel.getWidgetCount() == 0);
				}

				if (StoreComponentController.categoriesComponent != null && StoreComponentController.categoriesComponent.getCategoryModel().getId() > 0)
				{
					addCategoryBreadcrumb(StoreComponentController.categoriesComponent.getCategoryModel(), titlePanel.getWidgetCount() == 0);
				}

				if (StoreComponentController.manufacturersComponent.getManufacturerModel().getId() > 0)
				{
					if (titlePanel.getWidgetCount() > 0) addDelimiter();
					//TBD - id is future use.
					addBreadcrumb("ManufacturerComponent--" + StoreComponentController.manufacturersComponent.getManufacturerModel().getId(), StoreComponentController.manufacturersComponent.getManufacturerModel().getName(), true);
				}
			}
		}

		int titleCount = titlePanel.getWidgetCount();
		if (titleCount > 0) addDelimiter();
		addBreadcrumb("CategoriesSummaryComponent--0", storeController.getStoreConstants().BreadcrumbComponentStore(), titleCount == 0);
		addDelimiter();
		titlePanel.insert(homeAnchor, 0);

		//Welcome
		welcomePanel.clear();
		if (StoreComponentController.accountComponent != null)
		{
			AccountModel cmodel = StoreComponentController.accountComponent.getAccountModel();
			if (cmodel.getId() > 0)
			{
				InlineLabel welcomeLabel = new InlineLabel(storeController.getStoreConstants().BreadcrumbComponentWelcome() + " " + cmodel.getEmailAddress());
				welcomeLabel.setStyleName(getHandle() + "Welcome");
				welcomePanel.setCellHorizontalAlignment(welcomeLabel, HasHorizontalAlignment.ALIGN_RIGHT);
				welcomePanel.add(welcomeLabel);
			}
		}

		onBuilt();
	}

	protected void addBreadcrumb(String id, String name, boolean deadlink)
	{
		InlineLabel label = new InlineLabel(name);
		label.addStyleName(getHandle() + "Link");
		if (deadlink)
			label.addStyleName(getHandle() + "DeadLink");
		else
			label.addClickHandler(clickHandler);

		titlePanel.insert(label, 0);
		hash.put(name, id);
	}

	protected void addDelimiter()
	{
		InlineHTML delimiterLabel = new InlineHTML();
		delimiterLabel.setHTML(delimiter);
		delimiterLabel.addStyleName(getHandle() + "Delimiter");
		titlePanel.insert(delimiterLabel, 0);
	}

	protected void addCategoryBreadcrumb(CategoryModel model, boolean first)
	{
		int cid = model.getId();
		if (cid > 0) {
			String strName = model.getName();
			if (titlePanel.getWidgetCount() > 0) addDelimiter();
			addBreadcrumb("CategoriesSummaryComponent--" + String.valueOf(cid), strName, first);
			addCategoryBreadcrumb(model.getParentModel(), false);
		}
	}

	protected ClickHandler clickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			Widget source = (Widget)event.getSource();
			InlineLabel label = (InlineLabel)source;
			String strLabel = label.getText();
			String strId = hash.get(strLabel);

			onSelection(strId);
		}
	};
	public String getToken() {
		return token;
	}


	public void setToken(String token) {
		this.token = token;
	}
}
